﻿Imports System.IO
Public Class frmValidaLayout

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        With OpenFileDialog1
            .Filter = "Ficheros de texto (*.txt)|*.txt"
            If .ShowDialog = Windows.Forms.DialogResult.OK Then
                txtFile.Text = OpenFileDialog1.FileName
                txtDetalle.Text = AbrirArchivo(txtFile.Text)
            End If
        End With
    End Sub
    Public Function AbrirArchivo(ByVal Ruta As String)
        Dim objReader As New StreamReader(Ruta)
        Dim sLine As String = ""
        Dim Texto As String = ""
        Dim arrText As New ArrayList()
        Dim nuevoTexto As String
        Dim Nombre As String = ""
        Dim ApellidoP As String = ""
        Dim ApellidoM As String = ""
        Dim Parte1 As String = ""
        Dim Parte2 As String = ""
        Dim Parte3 As String = ""
        Dim LargoParte As Integer = 0
        Dim Faltan As Integer = 0

        Do
            sLine = objReader.ReadLine()
            If Not sLine Is Nothing Then
                If CInt(Mid(sLine, 1, 1)) = 3 Then
                    Nombre = Mid(sLine, 67, 89)
                    Nombre = Mid(Nombre, 1, 23)
                    ApellidoP = Mid(sLine, 91, 105)
                    ApellidoP = Mid(ApellidoP, 1, 15)
                    ApellidoM = Mid(sLine, 107, 121)

                    Nombre = Trim(Nombre)
                    ApellidoP = Trim(ApellidoP)
                    ApellidoM = Trim(ApellidoM)

                    Parte1 = Mid(sLine, 1, 66)
                    Parte3 = Mid(sLine, 122)

                    Parte2 = Nombre & "," & ApellidoP & "/" & ApellidoM
                    LargoParte = Len(Parte2)
                    Faltan = 55 - LargoParte

                    For i = 1 To Faltan
                        Parte2 = Parte2 & " "
                    Next


                    nuevoTexto = Parte1 & Parte2 & Parte3
                Else
                    nuevoTexto = sLine
                End If

                arrText.Add(nuevoTexto + vbCrLf)
            End If
        Loop Until sLine Is Nothing
        objReader.Close()
        For Each sLine In arrText
            Texto += sLine
        Next

       


        Return Texto

       
    End Function

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        'If Not Len(txtFile.Text) = 0 Then
        'With SaveFileDialog1
        ' .Reset()
        ' .Filter = "Ficheros de texto (*.txt)|*.txt"
        ' If .ShowDialog = Windows.Forms.DialogResult.OK Then
        ' If My.Computer.FileSystem.FileExists(.FileName) Then
        ' My.Computer.FileSystem.DeleteFile(.FileName)
        ' My.Computer.FileSystem.WriteAllText(.FileName, txtFile.Text & vbCrLf, True)
        ' My.Computer.FileSystem.WriteAllText(.FileName, txtFile.Text & vbCrLf, True)
        ' End If
        ' txtFile.Text = .FileName
        ' End If
        ' End With
        ' End If
        ' End If
        'Para Guardar Una Manera Sencilla Es Eliminar Y Crearlo
        ' Lo Eliminamos
        My.Computer.FileSystem.DeleteFile(txtFile.Text)
        ' Lo Creamos
        My.Computer.FileSystem.WriteAllText(txtFile.Text, txtDetalle.Text & vbCrLf, True)

    End Sub

    Private Sub frmValidaLayout_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ToolStrip1.Cursor = Cursors.Hand
        ToolStrip1.RenderMode = System.Windows.Forms.ToolStripRenderMode.System

        txtDetalle.Clear()
        txtFile.Clear()

    End Sub

    Private Sub ToolStripButton5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton5.Click
        My.Computer.FileSystem.DeleteFile(txtFile.Text)
        ' Lo Creamos
        My.Computer.FileSystem.WriteAllText(txtFile.Text, txtDetalle.Text & vbCrLf, True)

        Close()

    End Sub

    Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
        Close()

    End Sub
End Class